package com.langyiquan.front.controller;

import com.langyiquan.exception.http.ParamException;
import com.langyiquan.service.CategoryService;
import com.langyiquan.utils.PagedGridResult;
import com.langyiquan.utils.UnifyResponse;
import com.langyiquan.vo.CategoryVO;
import com.langyiquan.vo.NewItemsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "分类", tags = {"分类的相关接口"})
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "获取商品子分类",notes = "获取商品子分类",httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public UnifyResponse subCatgorys(@ApiParam(name = "rootCatId",value = "一级分类Id", required = true) @PathVariable Integer rootCatId){

        if(rootCatId == null){
            throw new ParamException(4001);
        }

        List<CategoryVO> result = categoryService.getSubCatList(rootCatId);
        return UnifyResponse.success(result);
    }


    @ApiOperation(value = "查询分类所有最新商品",notes = "查询分类所有最新商品",httpMethod = "GET")
    @GetMapping("/getItems/{catId}")
    public UnifyResponse getItemsByCatId(@ApiParam(name = "catId",value = "分类Id", required = true) @PathVariable Integer catId, @ApiParam(name = "catType",value = "分类类型:0代表根分类,1代表其他分类", required = true)  @RequestParam Integer catType, @RequestParam Integer page, @RequestParam Integer pageSize){

        if(catId == null){
            throw new ParamException(4001);
        }

        if(catType == null){
            catType = 0;
        }

        PagedGridResult pagedGridResult = categoryService.getItemsByCatId(catId,catType,page,pageSize);
        return UnifyResponse.success(pagedGridResult);
    }

}
